angular.module('starter')

    // .controller('AppCtrl', function() {})
    // .controller('LoginCtrl', function() {})
    // .controller('DashCtrl', function() {});
    .controller('AppCtrl', function ($scope, $state, $ionicPopup, AuthService, AUTH_EVENTS) {
        $scope.username = AuthService.username();

        $scope.$on(AUTH_EVENTS.notAuthorized, function (event) {
            var alertPopup = $ionicPopup.alert({
                title: 'Unauthorized!',
                template: 'You are not allowed to access this resource.'
            });
        });

        $scope.$on(AUTH_EVENTS.notAuthenticated, function (event) {
            AuthService.logout();
            $state.go('login');
            var alertPopup = $ionicPopup.alert({
                title: 'Session Lost!',
                template: 'Sorry, You have to login again.'
            });
        });

        $scope.setCurrentUsername = function (name) {
            $scope.username = name;
        };
    })

    .controller('LoginCtrl', function ($scope, $state, $ionicPopup, AuthService) {
        $scope.data = {};

        $scope.login = function (data) {
            AuthService.login(data.username, data.password).then(function (authenticated) {
                $state.go('main.house', {}, {reload: true});
                $scope.setCurrentUsername(data.username);
            }, function (err) {
                var alertPopup = $ionicPopup.alert({
                    title: '登录失败!',
                    template: '账号或密码错误!'
                });
            });
        };
    })

    .controller('HouseCtrl', function ($scope, $state, $http, $ionicPopup, AuthService, $ionicModal, ionicDatePicker, $filter) {
        $scope.logout = function () {
            AuthService.logout();
            $state.go('login');
        };
        // 房屋属性
        $scope.house = {};
        $scope.house.entire = {};

        // 几室
        $scope.roomsSelect = [{id: 1, value: '1'}, {id: 2, value: '2'}, {id: 3, value: '3'}, {
            id: 4,
            value: '4'
        }, {id: 5, value: '5'}];
        $scope.house.room = $scope.roomsSelect[0].id;// 默认选中第一个
        // 几厅
        $scope.livingRoomsSelect = [{id: 1, value: '1'}, {id: 2, value: '2'}, {id: 3, value: '3'}];
        $scope.house.livingRoom = $scope.livingRoomsSelect[0].id;

        // 朝向
        $scope.orientationSelect = [{id: 1, value: '东'}, {id: 2, value: '南'}, {id: 3, value: '西'}, {id: 4, value: '北'}];
        $scope.house.orientation = $scope.orientationSelect[0].id;

        // 装修程度
        $scope.decorationSelect = [{id: 1, value: '简装'}, {id: 2, value: '精装'}];
        $scope.house.decoration = $scope.decorationSelect[0].id;

        // 租赁方式
        $scope.typeSelect = [{id: 1, value: '合租'}, {id: 2, value: '整租'}];
        $scope.house.type = $scope.typeSelect[0].id;

        $scope.goEntire = function () {
            $state.go('main.entire', {
                value: $scope.house
            }, {
                reload: true
            });
        }


        var dateOptions = {
            // 从哪天开始
            from: new Date(),
            callback: function (val) {
                $scope.house.entire.startDate = $filter('date')(val, 'yyyy-MM-dd');
            }
        };
        $scope.openDatePicker = function () {
            // 关闭掉键盘
            if (window.cordova && window.cordova.plugins.Keyboard) {
                window.cordova.plugins.Keyboard.close(100);
            }
            document.getElementsByName('startDate')[0].blur();
            ionicDatePicker.openDatePicker(dateOptions);
        };

        $scope.performValidRequest = function () {
            $http.get('http://localhost:8100/valid').then(
                function (result) {
                    $scope.response = result;
                });
        };

        $scope.performUnauthorizedRequest = function () {
            $http.get('http://localhost:8100/notauthorized').then(
                function (result) {
                    // No result here..
                }, function (err) {
                    $scope.response = err;
                });
        };

        $scope.performInvalidRequest = function () {
            $http.get('http://localhost:8100/notauthenticated').then(
                function (result) {
                    // No result here..
                }, function (err) {
                    $scope.response = err;
                });
        };
    })
    .controller('houseEntireCtrl', function ($scope, $state, $http, $ionicPopup, AuthService, $ionicModal) {
    });
